package club.banyuan;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "Servlet1", urlPatterns = "/s1")
public class Servlet1 extends HttpServlet {
    @Override
    public void init() throws ServletException {
        super.init();
        System.out.println("init");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 重定向
        // 有"/"，就是指根目录
//        response.sendRedirect("/index.jsp");

        // request.getContextPath() 得到的是项目的虚拟路径
        response.sendRedirect(request.getContextPath() + "/index.jsp");
//        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
//        response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
//        response.sendError(HttpServletResponse.SC_BAD_REQUEST,"Bad Request");
//        response.setHeader("Location" , "index.jsp");

//        response.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=UTF-8");
//        String html = "<html><body><div>测试</div></body></html>";
//        response.getWriter().write(html);
//        response.getOutputStream().write(html.getBytes());
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        super.service(req, res);
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("destroy");
    }
}
